package com.sgb.kjwl.view.ui.activity.packers;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityWorkEquipmentBinding;
import com.sgb.kjwl.di.packers.DaggerPackersComponent;
import com.sgb.kjwl.di.packers.PackersModule;
import com.sgb.kjwl.model.entity.self.SelfTeamDeviceEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamDeviceEditActivity;
import com.sgb.kjwl.view.ui.adapter.packers.WorkEquipmentAdapter;
import com.sgb.kjwl.viewmodel.packers.WorkEquipmentModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.inject.Inject;

/**
 * @date 2021/3/15
 * @author F_Q
 * Description: 选择工队设备
 */
public class WorkEquipmentActivity extends AppActivity {

    private static final String TEAM_NO = "teamNo";
    private ActivityWorkEquipmentBinding mBind;
    @Inject
    WorkEquipmentModel viewModel;
    private final int REQUEST_ADD_CODE = 105;
    private WorkEquipmentAdapter workequipmentadapter;
    private boolean isLoadMore;
    private int page;

    public static void start(Context context,  String teamNo) {
        Intent intent = new Intent();
        intent.putExtra(TEAM_NO, teamNo);
        intent.setClass(context, WorkEquipmentActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPackersComponent.builder().appComponent(appComponent).packersModule(new PackersModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        mBind = DataBindingUtil.setContentView(this, R.layout.activity_work_equipment);
        mBind.setViewModel(viewModel);
        mBind.results.setOnClickListener(this);
        mBind.addWorkEquipment.setOnClickListener(this);
        mBind.allEquipmentSelected.setOnClickListener(this);
        adapterData();
    }

    /**
     * LRecyclerView-adapter初始化
     */
    private void adapterData() {
        workequipmentadapter = new WorkEquipmentAdapter(this, null);
        LRecyclerViewAdapter mLRecyclerViewAdapter = new LRecyclerViewAdapter(workequipmentadapter);
        mBind.lrv.setLayoutManager(new LinearLayoutManager(this));
        mBind.lrv.setAdapter(mLRecyclerViewAdapter);
        mBind.lrv.setOnRefreshListener(() -> {
            //下拉刷新
            isLoadMore = false;
            page = 1;
            viewModel.refreshAndLoadMore(page,  getIntent().getStringExtra(TEAM_NO));
        });
        mBind.lrv.setOnLoadMoreListener(() -> {
            //加载更多
            isLoadMore = true;
            page++;
            viewModel.refreshAndLoadMore(page, getIntent().getStringExtra(TEAM_NO));
        });
        //点击CheckedBox更新数据
        workequipmentadapter.setCheckedbos(new WorkEquipmentAdapter.OnCheckedChangeCheckBox() {
            @Override
            public void selectData() {
                setButtonTextView();
            }

            @Override
            public void detailsData(String id,String no) {
                viewModel.getH5Url(no);
            }
        });
    }

    @Override
    protected void init() {
        //首次接口数据请求
        isLoadMore = false;
        page = 1;
        viewModel.refreshAndLoadMore(page, getIntent().getStringExtra(TEAM_NO));
    }

    @Override
    public void onMultiClick(View v) {
        super.onMultiClick(v);
        int id = v.getId();
        if (id == R.id.iv_back) {
            viewModel.TeamParticulars(getIntent().getStringExtra(TEAM_NO));
        } else if (id == R.id.results) {
            //跳转到工队设备发布
            SelfTeamDeviceEditActivity.start(this, null, REQUEST_ADD_CODE);
        } else if (id == R.id.add_work_equipment) {
            //提交已选择的工队设备数据
            //返回重新加载列表界面
            addWorkEquipmentData();
        } else if (id == R.id.all_equipment_selected) {
            if (mBind.allEquipmentSelected.isChecked()) {
                //全部选中处理
                addSelectedList();
            } else {
                //全部取消处理
                cancelSelectedList();
            }
        }
    }

    private void addWorkEquipmentData() {
        List<String> equipmentList = new ArrayList<>();
        for (SelfTeamDeviceEntity data : workequipmentadapter.getDatas()) {
            if (data.isIslooks()) {
                equipmentList.add(data.getNo());
            }
        }
        // 类型 1 人员；2 设备；3 业绩； 4 工队相册；5 施工风采
        viewModel.setAddRelation(equipmentList,getIntent().getStringExtra(TEAM_NO),"2");
    }

    /**
     * 全选处理
     */
    private void addSelectedList() {
        for (SelfTeamDeviceEntity data : workequipmentadapter.getDatas()) {
            data.setIslooks(true);
        }
        workequipmentadapter.notifyDataSetChanged();
        setButtonTextView();
    }

    /**
     * 全部取消处理
     */
    private void cancelSelectedList() {
        for (SelfTeamDeviceEntity data : workequipmentadapter.getDatas()) {
            data.setIslooks(false);
        }
        workequipmentadapter.notifyDataSetChanged();
        setButtonTextView();

    }

    /**
     * 返回数据处理
     * @param data 列表的数据
     */
    public void setWorkEquipmentData(List<SelfTeamDeviceEntity> data) {
        if (data == null) {
            return;
        }
        mBind.lrv.refreshComplete(page);
        mBind.lrv.setPullRefreshEnabled(true);
        if (isLoadMore) {
            if (data.size() > 0) {
                workequipmentadapter.addDatas(data);
            } else {
                MToast.showToast(getApplicationContext(), "没有数据信息啦！");
            }
        } else {
            if (data.size() > 0) {
                mBind.listView.setVisibility(View.VISIBLE);
                mBind.emptyView.setVisibility(View.GONE);
                workequipmentadapter.setDatas(data);
            } else {
                mBind.listView.setVisibility(View.GONE);
                mBind.emptyView.setVisibility(View.VISIBLE);
                workequipmentadapter.setDatas(null);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_ADD_CODE) {
                //重新获取工队设备列表信息
                init();
            }
        }
    }

    /**
     * 接口错误或者无数据加载布局展示
     */
    public void showEmpty() {
        mBind.listView.setVisibility(View.GONE);
        mBind.emptyView.setVisibility(View.VISIBLE);
    }

    /**
     * 提交完数据后处理逻辑
     * @param url h5的网页结果
     */
    public void setUrlView(String url) {
        WebViewX5HostActivity.start(WorkEquipmentActivity.this, url);
        finish();
    }
    public  void  setButtonTextView(){
        int selectedNum = 0;
        for (SelfTeamDeviceEntity data : workequipmentadapter.getDatas()) {
            if (data.isIslooks()){
                selectedNum++;
            }
        }

        mBind.addWorkEquipment.setText(String.format(Locale.CHINA,"添加工队设备（%d）",selectedNum));
    }
}
